package pwd.allen.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import pwd.allen.mapper.CommonMapper;
import pwd.allen.service.IPagerService;
import pwd.allen.util.Pager;

import java.util.List;
import java.util.function.Function;

/**
 * @author lenovo
 * @create 2020-09-09 10:35
 **/
public class BaseService<M extends CommonMapper<T>, T> extends ServiceImpl<M, T> implements IPagerService<T> {

    @Override
    public IPage<T> selectByPager(Pager<T> pager) {
        return baseMapper.selectByPager(pager);
    }

    public static <E> int execBatch(List<E> list, Function<List<E>, Integer> function, Integer perSize) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        } else {
            if (perSize == null || perSize <= 0) {
                perSize = 1000;
            }

            int suc = 0;
            int fromIndex = 0;

            int toIndex;
            for(boolean var5 = false; fromIndex < list.size(); fromIndex = toIndex) {
                toIndex = fromIndex + perSize > list.size() ? list.size() : fromIndex + perSize;
                suc += (Integer)function.apply(list.subList(fromIndex, toIndex));
            }
            return suc;
        }
    }
}
